﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using com.eforceglobal.crux.configuration;

public class Queries
{
    private BusinessObjectsforDemoDataContext bc = new BusinessObjectsforDemoDataContext(ConnectionString);

    public void InsertPurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
    {
        var purchaseOrder = new PurchaseOrder
        {
            ItemName = purchaseOrderInfo.ItemName,
            Itemquantity = purchaseOrderInfo.Itemquantity,
            PurchaseOrderDate = purchaseOrderInfo.PurchaseOrderDate,
            VendorId = purchaseOrderInfo.VendorId,
            Price = purchaseOrderInfo.Price,
            IsCredit = purchaseOrderInfo.IsCredit,
            WorkflowInstanceId = purchaseOrderInfo.WorkflowInstanceId,
        };

        bc.PurchaseOrders.InsertOnSubmit(purchaseOrder);
        bc.SubmitChanges();
    }
    public void ModifyPurchaseOrder(PurchaseOrderInfo purchaseOrderInfo)
    {
        PurchaseOrder purchaseOrder = bc.PurchaseOrders.Where(p => p.PurchaseOrderId == purchaseOrderInfo.PurchaseOrderId).FirstOrDefault();
        purchaseOrder.ItemName = purchaseOrderInfo.ItemName;
        purchaseOrder.Itemquantity = purchaseOrderInfo.Itemquantity;
        purchaseOrder.PurchaseOrderDate = purchaseOrderInfo.PurchaseOrderDate;
        purchaseOrder.Price = purchaseOrderInfo.Price;
        purchaseOrder.IsCredit = purchaseOrderInfo.IsCredit;
        bc.SubmitChanges();
    }
    public List<PurchaseOrderInfo> GetPurchaseOrderInfo()
    {
        return (from purchaseOrderList in bc.PurchaseOrders
                from workflowInstanceList in bc.WorkflowInstances
                where
                    purchaseOrderList.WorkflowInstanceId == workflowInstanceList.WorkflowInstanceId
                select new PurchaseOrderInfo
                {
                    PurchaseOrderId = purchaseOrderList.PurchaseOrderId,
                    ItemName = purchaseOrderList.ItemName,
                    Itemquantity = purchaseOrderList.Itemquantity,
                    PurchaseOrderDate = purchaseOrderList.PurchaseOrderDate,
                    VendorId = purchaseOrderList.VendorId,
                    Price = purchaseOrderList.Price,
                    IsCredit = purchaseOrderList.IsCredit,
                    WorkflowInstanceId = purchaseOrderList.WorkflowInstanceId,
                    WorkFlowDate = workflowInstanceList.LastModifiedOn,
                    WorkFlowState = workflowInstanceList.StateName
                }
             ).ToList();
    }
    public List<PurchaseOrderInfo> GetPurchaseOrderInfoByStateName(string stateName)
    {
        return (from purchaseOrderList in bc.PurchaseOrders
                from workflowInstanceList in bc.WorkflowInstances
                where purchaseOrderList.WorkflowInstanceId == workflowInstanceList.WorkflowInstanceId
                where workflowInstanceList.StateName.Equals(stateName)
                select new PurchaseOrderInfo
                {
                    PurchaseOrderId = purchaseOrderList.PurchaseOrderId,
                    ItemName = purchaseOrderList.ItemName,
                    Itemquantity = purchaseOrderList.Itemquantity,
                    PurchaseOrderDate = purchaseOrderList.PurchaseOrderDate,
                    VendorId = purchaseOrderList.VendorId.GetValueOrDefault(),
                    Price = purchaseOrderList.Price,
                    IsCredit = purchaseOrderList.IsCredit,
                    WorkFlowDate = workflowInstanceList.LastModifiedOn,
                    WorkFlowState = workflowInstanceList.StateName,
                    WorkflowInstanceId = purchaseOrderList.WorkflowInstanceId,
                }
             ).ToList();
    }
    public PurchaseOrderInfo PurchaseOrdersByWorkFlowInstanceId(int instId)
    {
        return (from purchaseOrderList in bc.PurchaseOrders
                where purchaseOrderList.WorkflowInstanceId == instId
                select new PurchaseOrderInfo
                {
                    PurchaseOrderId = purchaseOrderList.PurchaseOrderId,
                    ItemName = purchaseOrderList.ItemName,
                    Itemquantity = purchaseOrderList.Itemquantity,
                    PurchaseOrderDate = purchaseOrderList.PurchaseOrderDate,
                    VendorId = purchaseOrderList.VendorId,
                    Price = purchaseOrderList.Price,
                    IsCredit = purchaseOrderList.IsCredit,
                    WorkflowInstanceId = purchaseOrderList.WorkflowInstanceId,
                }
               ).FirstOrDefault();
    }
    private static string ConnectionString
    {
        get
        {
            return CruxConfigurationManager.GetAdditionalDatabaseConnectionString("WorkFlowDB");
        }
    }
}
